/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
define(["mini/weavee/BaseObject", "text!mini/weavee/DataGrid.html"], function(BaseObject, UI){
    return function(){
        var parent=new BaseObject(UI);
        jQuery.extend(this, parent);
        
        this.model=null;
        /**
         * 
         * @param {type} _model instance of mini/weavee/DataGridModel
         * @returns {undefined}
         */
        this.setModel=function(_model){
            this.model=_model;
        }
        this.getModel=function(){
            return this.model;
        }
        this.repaint=function(){
            if(this.model){
                //clear
                var ui=this.ui;
                $(ui).find("._datagrid_header_column_").each(function(){
                    this.parentNode.removeChild(this);
                });
                $(ui).find("._datagrid_header_column_control_").each(function(){
                    this.parentNode.removeChild(this);
                });
                $(ui).find("._datagrid_data_").each(function(){
                    this.parentNode.removeChild(this);
                });
                //process mini model
                if(this.model.getMiniModelName()){
                    $($(this.ui).find("._datagrid_").get(0)).attr("mini:model", this.model.getMiniModelName());
                }
                //add columns
                for(var i in this.model.getColumns()){
                    var th=document.createElement("th");
                    $(th).addClass("_datagrid_header_column_");
                    $(th).text(this.model.getColumns()[i].getLabel());
                    $(this.ui).find("._datagrid_header_").get(0).appendChild(th);
                }
                //add control column
                var th=document.createElement("th");
                $(th).addClass("_datagrid_header_column_control_");
                $(this.ui).find("._datagrid_header_").get(0).appendChild(th);
                //add rows
                for(var i in this.model.getRows()){
                    var row=this.model.getRows()[i];
                    var tr = document.createElement("tr");
                    if (this.model.getMiniModelName()) {
                        $(tr).attr("mini:object", "data");
                    }
                    for (var j in row) {
                        var td=document.createElement("td");
                        $(td).addClass("_datagrid_data_column_");
                        $(td).text(row[j]);
                        if (this.model.getMiniModelName()) {
                            if(this.model.getColumns()[j].getBindingText()){
                                $(td).attr("mini:text", this.model.getColumns()[j].getBindingText());
                            }
                            if(this.model.getColumns()[j].getBindingValue()){
                                $(td).attr("mini:value", this.model.getColumns()[j].getBindingValue());
                            }
                        }
                        tr.appendChild(td);
                    }
                    var td=document.createElement("td");
                    $(td).addClass("_datagrid_data_column_control_");
                    $(td).html("<input type=\"button\" class=\"_datagrid_data_column_control_edit_\" value=\"EDIT\"/><input type=\"button\" class=\"_datagrid_data_column_control_delete_\" value=\"DELETE\"/>");
                    tr.appendChild(td);
                    $(this.ui).find("._datagrid_").get(0).appendChild(tr);
                }
            }
            //parent.repaint();
        }
        return this;
    }
});
